<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        function deepCopy(obj1) {
            // var obj2 = {};
            // var obj2 = [];
            // 根据传入的数据自动生成数组或者是对象
            var obj2 = Object.prototype.toString.call(obj1)==="[object Array]"?[]:{};
            for (var key in obj1) {
                // 把obj1里的键名和键值加入到obj2里
                obj2[key] = obj1[key];
            }
            return obj2;
        }
        // var arr = [1,2,3];
        // var obj = {name:"张三",age:20}
        var obj=[1,2]
        var newarr =  deepCopy(obj);
        console.log(newarr);
        console.log(Object.prototype.toString.call(newarr))

    </script>
</body>
</html>